package com.tangyee.home_knowledge.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangyee.R;
import com.tangyee.home_knowledge.bean.KnowledgeItem;
import com.tangyee.home_knowledge.util.ConstantUtil;
import com.tangyee.home_knowledge.util.HttpUtil;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**  1
 * @author: yu huawen
 * @date: 2019/1/24 10:05
 */
public class KnowledgeListViewAdapter extends BaseAdapter {
    // 上下文
    private Context context;
    // 知识的布局id
    private int knowledgeItemLayoutId;
    // 知识列表
    private List<KnowledgeItem> knowledgeItemList;
    public final String imagePreURL = "https://www.tangyee.com/Content/upload/images/RecipeImages/";

    public KnowledgeListViewAdapter(Context context, int knowledgeListId, List<KnowledgeItem> list){
        this.context = context;
        this.knowledgeItemLayoutId = knowledgeListId;
        this.knowledgeItemList = list;
    }
    @Override
    public int getCount() {
        return knowledgeItemList == null ? 0 : knowledgeItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return knowledgeItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public List<KnowledgeItem> getKnowledgeItemList() {
        return knowledgeItemList;
    }

    public void setKnowledgeItemList(List<KnowledgeItem> knowledgeItemList) {
        this.knowledgeItemList = knowledgeItemList;
    }

    private Handler handler;
    @SuppressLint("HandlerLeak")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(knowledgeItemLayoutId, null);
        KnowledgeItem item = knowledgeItemList.get(position);
        ImageView imageView = (ImageView)view.findViewById(R.id.ImageKnows);
        TextView title = (TextView)view.findViewById(R.id.KnowsTitle);
        TextView time = (TextView)view.findViewById(R.id.KnowsTime);
        TextView cont = (TextView)view.findViewById(R.id.KnowsContent);

        title.setText(item.getTitle());
        time.setText(item.getReleaseDate());
        setImage(item.getImage(), new ImageHandler(imageView));
//        handler = new Handler(){
//            @Override
//            public void handleMessage(Message msg) {
//                switch (msg.what) {
//                    // 加载网络成功进行UI的更新,处理得到的图片资源
//                    case ConstantUtil.SUCCESS:
//                        // 通过message，拿到字节数组
//                        byte[] picture = (byte[]) msg.obj;
//                        // 使用BitmapFactory工厂，把字节数组转化为bitmap
//                        Bitmap bitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);
//                        // 通过imageview，设置图片
//                        imageView.setImageBitmap(bitmap);
//                        break;
//                    // 当加载网络失败执行的逻辑代码
//                    case ConstantUtil.FALL:
//                        break;
//                }
//            }
//        };

        return view;
    }

    private void setImage(String imageName, final Handler handler){

        HttpUtil.downloadImage(imagePreURL + imageName, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // 得到从请求获取资源，转换成我们想要的类型
                byte[] picture_bt = response.body().bytes();
                // 通过handler更新UI
                Message message = handler.obtainMessage();
                message.obj = picture_bt;
                message.what = ConstantUtil.SUCCESS;
                handler.sendMessage(message);
            }
        });
    }

    class ImageHandler extends Handler {

        private ImageView imageView;

        public ImageHandler(ImageView imageView) {
            this.imageView = imageView;
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 加载网络成功进行UI的更新,处理得到的图片资源
                case ConstantUtil.SUCCESS:
                    // 通过message，拿到字节数组
                    byte[] picture = (byte[]) msg.obj;
                    // 使用BitmapFactory工厂，把字节数组转化为bitmap
                    Bitmap bitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length);
                    // 通过imageview，设置图片
                    imageView.setImageBitmap(bitmap);
                    break;
                // 当加载网络失败执行的逻辑代码
                case ConstantUtil.FALL:
                    break;
            }
        }
    }

}
